﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace GUI
{
    public partial class HoaDonGUI : Window
    {

        private static int _MaHoaDon;
        public int MaHoaDon
        {
            get { return _MaHoaDon; }
            set { _MaHoaDon = value; }
        }
        public HoaDonGUI()
        {
            InitializeComponent();
            hoiThoat = false;
            dateTimePickerFrom.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            dateTimePickerTo.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
        }
        public void HoaDonGUI_Load(object sender, EventArgs e)
        {
            dataGridViewHoaDon.DataSource = null;
            dataGridViewHoaDon.Rows.Clear();
            dataGridViewHoaDon.Refresh();
            BUS.HoaDonBUS hd = new HoaDonBUS();
            dataGridViewHoaDon.DataSource = hd.SelectAll(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month))).Tables[0];
            #region Đặt tên cho cột
            dataGridViewHoaDon.Columns[0].HeaderText = "Tình trạng";
            dataGridViewHoaDon.Columns[1].HeaderText = "Người Lập H.Đ";
            dataGridViewHoaDon.Columns[2].HeaderText = "Mã hóa đơn";
            dataGridViewHoaDon.Columns[3].HeaderText = "Ngày lập H.Đ";
            dataGridViewHoaDon.Columns[4].Visible = false; //SoHD
            dataGridViewHoaDon.Columns[5].HeaderText = "Mã K.H";
            dataGridViewHoaDon.Columns[6].HeaderText = "Tên K.H";
            dataGridViewHoaDon.Columns[7].HeaderText = "Địa chỉ";
            dataGridViewHoaDon.Columns[8].HeaderText = "Thành tiền";
            dataGridViewHoaDon.Columns[9].Visible = false; //MaNV
            dataGridViewHoaDon.Columns[10].Visible = false; //LoaiKH
            dataGridViewHoaDon.Columns[11].HeaderText = "Ghi Chú";
            dataGridViewHoaDon.Columns[12].Visible = false; //TT
            #endregion
           
           
        }
        public void LoadData()
        {
           
            DateTime st = dateTimePickerFrom.Value;
            DateTime en = dateTimePickerTo.Value;
            BUS.HoaDonBUS hd = new HoaDonBUS();
            dataGridViewHoaDon.DataSource = hd.SelectAll(st, en).Tables[0];
        }
        public void btnXem_Click(object sender, EventArgs e)
        {
            this.LoadData();
        }
        private void dateTimePickerFrom_ValueChanged(object sender, EventArgs e)
        {
            dateTimePickerTo.MinDate = dateTimePickerFrom.Value;
        }
        private void button1_Click(object sender, EventArgs e)
        {

            ThemHoaDonGUI frm = new ThemHoaDonGUI();
            frm.MdiParent = this.MdiParent;
            frm.Show();
            
        }
        void XemCT()
        {
            if (dataGridViewHoaDon.RowCount == 0)
                return;
            DataGridViewCell cell;
            DataGridViewRow row=dataGridViewHoaDon.CurrentRow;
            cell = row.Cells[2];
            HoaDonBUS bus=new HoaDonBUS();
            //if (bus.IsExport(int.Parse(cell.Value.ToString())) == 1)
            //    return;
            int MaHD_DuocChon=int.Parse(cell.Value.ToString());
            FormXemChiTietHoaDom frm = new FormXemChiTietHoaDom(MaHD_DuocChon);
            frm.MdiParent = this.MdiParent;
            frm.Show();
            
        }
        private void btnXemCT_Click(object sender, EventArgs e)
        {
            this.XemCT();
        }
        private void btnXoaHD_Click(object sender, EventArgs e)
        {
            if (dataGridViewHoaDon.RowCount == 0)
                return;
            DataGridViewCell cell;
            cell = dataGridViewHoaDon.CurrentRow.Cells[2];
            if (cell == null)
                return;
            if (MessageBox.Show("Ban Muon Xoa ?", "Thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;
            int maHD=int.Parse(cell.Value.ToString());
            CTHoaDonBUS bus = new CTHoaDonBUS();
            List<CTHoaDonDTO> lst = bus.LayChiTietHoaDon(maHD);
            for (int i = 0; i < lst.Count; i++)
            {
                bus.XoaCTHD(lst[i]);
            }
            HoaDonBUS hdBUS = new HoaDonBUS();
            hdBUS.HuyHoaDon(maHD);
            this.LoadData();
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void dataGridViewHoaDon_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.XemCT();
        }
        private void btnIn_Click(object sender, EventArgs e)
        {
            if (dataGridViewHoaDon.RowCount == 0)
                return;
            DataGridViewCell cell;
            cell = dataGridViewHoaDon.CurrentRow.Cells[2];
            frmInBaoCao frm = new frmInBaoCao();
            int maHD=int.Parse(cell.Value.ToString());
            frm.MaHD =maHD ;
            frm.MdiParent = this.MdiParent;
            frm.Show();
            HoaDonBUS hdBUS = new HoaDonBUS();
            hdBUS.UpdateTinhTrangHoaDon(maHD, true);
           
        }
        public void RefeshData()
        {
            dataGridViewHoaDon.DataSource = null;
            dataGridViewHoaDon.Rows.Clear();
            dataGridViewHoaDon.Refresh();
        }
      
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.RefeshData();
            this.HoaDonGUI_Load(null, null);
        }
    }
}
